﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class InboxMessageList : System.Web.UI.Page
{
    DataSet ds = null;
    int UserID = 0, IsPaid = 0, UserTypeID = 0, MessageID = 0;
    RegistrationData reg = new RegistrationData();
    Communication cs = new Communication();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["User"] != null)
            {
                BindSentMessages("");
                ViewState["SortFrom"] = "asc";
                ViewState["SortSubject"] = "asc";
                ViewState["SortDate"] = "asc";
            }
        }
    }
    void BindSentMessages(string Sort)
    {
        if (Session["User"] != null)
        {
            ds = new DataSet();
            reg = (RegistrationData)Session["User"];
            //UserTypeID = reg.UserTypeID;
            //IsPaid = reg.IsPaid;
            UserID = reg.UserID;
            cs.UserID = UserID;
            cs.MessageID = 0;
            cs.MessageType = "";
            ds = cs.Select_Messages();
            if (Sort != "")
            {
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        DataRow[] dr = ds.Tables[0].Select(null, Sort);
                        ds.Tables.Remove(ds.Tables[0]);
                        ds.Merge(dr);
                    }
                }
            }
            lvSentMessages.DataSource = ds;
            lvSentMessages.DataBind();


        }
    }

    protected void gvNewFiles_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "deleteFile":
                if (Session["User"] != null)
                {
                    reg = (RegistrationData)Session["User"];
                    UserID = reg.UserID;
                    cs.UserID = UserID;
                    cs.CommunicationID = Convert.ToInt32(e.CommandArgument);
                    cs.Delete_InboxMessage();
                }
                BindSentMessages("");
                break;

        }
    }


     
    protected void lnkSubject_Click(object sender, EventArgs e)
    {
        if (ViewState["SortSubject"] != null)
        {
            BindSentMessages("Subject " + Convert.ToString(ViewState["SortSubject"]));
            if (Convert.ToString(ViewState["SortSubject"]) == "asc")
                ViewState["SortSubject"] = "desc";
            else ViewState["SortSubject"] = "asc";
        }
    }
    protected void lnkDate_Click(object sender, EventArgs e)
    {
        if (ViewState["SortDate"] != null)
        {
            BindSentMessages("Date " + Convert.ToString(ViewState["SortDate"]));
            ViewState["SortDate"] = "desc";
        }
    }
    protected void lnkFrom_Click(object sender, EventArgs e)
    {
        if (ViewState["SortFrom"] != null)
        {
            BindSentMessages("UserName " + Convert.ToString(ViewState["SortFrom"]));
            if (Convert.ToString(ViewState["SortFrom"]) == "asc")
                ViewState["SortFrom"] = "desc";
            else ViewState["SortFrom"] = "asc";
        }
    }
    protected void lvSentMessages_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            Label lblIsRead = (Label)e.Item.FindControl("lblIsRead");
            if (lblIsRead != null)
            {

                if (lblIsRead.Text == "0")
                {
                    System.Web.UI.HtmlControls.HtmlGenericControl divMain = (System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("divMain");
                    divMain.Style.Add("font-weight", "bold");
                }

            }
        }
    }
}